<?php

/**
 * @package YouNet_Controller_Tab
 * @version 1.1.0
 * @author LocNX <locnx@younetco.com>
 */
abstract class YouNet_Controller_Tab_Action extends YouNet_Controller_Action {
	
    /**
     * Id of an campaign. It can be id of a contest, a sweepstakes etc...
     * @var integer
     */
    private $_id_campaign;
    /**
     * Id of a Facebook Page. It will be set in init function
     * @var integer
     */
    protected $_id_page;

    protected $_is_allow_tracking_report = true;
    
    public function init() {
        parent::init();
        $this->_id_page = $this->_getParam('id_page',$this->_social->getIdPage());
        if($this->_request->isXmlHttpRequest()){
            $this->_is_allow_tracking_report = false;
        }
    }


    public function preDispatch() {
        parent::preDispatch();
        $this->initReportCampaign();
        if( $this->_is_allow_tracking_report )
        {
    		$this->initLikeRecord();
    		$this->initVisitorRecord();
        }
    }

    protected function setAllowTrackingReport($flag = true)
    {
        $this->_is_allow_tracking_report = $flag;
    }

    protected function disableTrackingReport()
    {
        $this->setAllowTrackingReport(false);
    }
    
    /**
     * This should be implemented for all extended of its.
     * Example:
     * <pre>
     * <code>
     * <?php
     * protected function initReportCampaign() {
     *      $this->setIdCampaign($this->_id_contest);     
     * }
     * ?>
     * </code>
     * </pre>
     */
    abstract protected function initReportCampaign();
    
    /**
     * 
     * @return integer
     * @throws ErrorException
     */
    protected function getIdCampaign()
    {
        if( empty($this->_id_campaign) )
        {
            $message = 'Please set id_campaign for: '.__CLASS__.' class';
            throw new ErrorException($message);
        }
        return $this->_id_campaign;
    }
    
    /**
     * 
     * @param integer $id_campaign
     */
    protected function setIdCampaign($id_campaign)
    {
        $this->_id_campaign = $id_campaign;
    }

	
    /**
     * Get array of params will be store for report feature
     * @return array
     */
    private function getParams()
    {
        $params                 = $this->_social->getVisitorInfo();
        $params['id_campaign']  = $this->getIdCampaign();
        return $params;
    }
	
	 /**
     * Get hash key from Visitor info to enhance seperating user
     * @return array
     */
	private function getKeyByVisitorInfo()
	{
		$params = $this->getParams();
		return md5($params['locale'] . $params['country'] . $params['min_age']. $params['max_age'] . $params['id_campaign']);
	}
    /**
     * Init visitor info
     */
    private function initVisitorRecord()
	{
		$likeKey = "trackLike_" . $this->getKeyByVisitorInfo();
		$visitorKey = "pageVisited_" . $this->getKeyByVisitorInfo();
        $id_page = $this->_id_page;
		$ns = Zend_Registry::get('session');
		/* @var $ns Zend_Session_Namespace */
		if(!isset($ns->{$visitorKey}) || !in_array($id_page,$ns->{$visitorKey}))
		{
			$params = $this->getParams();
			if (isset($ns->{$likeKey}[$id_page]) && !$ns->{$likeKey}[$id_page])
				$params['is_old_fan'] = 1;
			YouNet_Model_DbTable_Visitors::recordVisitor($params);
			if (!isset($ns->{$visitorKey}))
				$ns->{$visitorKey} = array($id_page);
			else 
				$ns->{$visitorKey}[] = $id_page;
		}
	}
    /**
     * Init like info
     */
	private function initLikeRecord()
	{
		$ns = Zend_Registry::get('session');
		$likeKey = "trackLike_" . $this->getKeyByVisitorInfo();
		$like_page = $this->_social->hasLikedPage();
		$id_page = $this->_id_page;
		if(!$like_page)
		{
			if (!isset($ns->{$likeKey}[$id_page]))
			{
				if (!isset($ns->{$likeKey}))
					$ns->{$likeKey} = array( $id_page => true);
				else
					$ns->{$likeKey}[$id_page] = true;
			}
		}
		else {
			if (isset($ns->{$likeKey}) && isset($ns->{$likeKey}[$id_page]) && $ns->{$likeKey}[$id_page])
			{
				$params = $this->getParams();
				$params['time_liked'] = $params['time_visited'] ;
				unset($params['time_visited']);
				YouNet_Model_DbTable_Likes::recordLike($params);
				$ns->{$likeKey}[$id_page] = false;
			}
			else
			{
				if (!isset($ns->{$likeKey}))
					$ns->{$likeKey} = array( $id_page => false);
				else
					$ns->{$likeKey}[$id_page] = false;
			}	
		}
	}
}